package aar.test.tessocr;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import aar.test.tessocr.adapter.OrderListAdapter;
import aar.test.tessocr.base.BaseActivity;
import aar.test.tessocr.baseApplication.BaseApplication;
import aar.test.tessocr.model.DialogCallback;
import aar.test.tessocr.model.LzyResponse;
import aar.test.tessocr.model.OrderModel;
import aar.test.tessocr.utils.Urls;
import clx.tessocr.ocreng.R;
import okgo.OkGo;
import okgo.model.Response;
import okhttp3.RequestBody;
import static aar.test.tessocr.Login1Activity.JSON;

/**
 * author: sunshine
 * created on: 2021/2/20 16:58
 * description:
 */
public class AppointmentListActivity extends BaseActivity {
    TextView tvNoData;
    XRecyclerView rvData;
    TextView tv_back;
    private List<OrderModel> mList = new ArrayList<>();
    private OrderListAdapter mAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_appointment_list);
        tvNoData = (TextView)findViewById(R.id.tv_no_data);
        rvData = (XRecyclerView) findViewById(R.id.rv_data);
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //WaitDialog.show(KeyListActivity.this, "网络请求中，请稍后...");
        getData();
    }

    private void getData() {
        /*KeyListModel keyListModel = new KeyListModel();
        keyListModel.setKeyNum("01");
        keyListModel.setKeyStatus("1");
        mList.add(keyListModel);

        KeyListModel keyListModel1 = new KeyListModel();
        keyListModel1.setKeyNum("02");
        keyListModel1.setKeyStatus("0");
        mList.add(keyListModel1);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        rvData.setLayoutManager(manager);
        mAdapter = new KeyListAdapter(this, mList);
        rvData.setAdapter(mAdapter);*/

        Map<String, Object> map = new HashMap<>();
        map.put("adminid", BaseApplication.getIntance().getLoginBean().getId());

        Gson gg = new Gson();
        String json = gg.toJson(map);

        RequestBody body = RequestBody.create(JSON, json);
        OkGo.<LzyResponse<List<OrderModel>>>post(Urls.URL_YUYUE)//
                .tag(this)//
                .upRequestBody(body)
                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                .execute(new DialogCallback<LzyResponse<List<OrderModel>>>(AppointmentListActivity.this) {
                    @Override
                    public void onSuccess(Response<LzyResponse<List<OrderModel>>> response) {
                        mList = new ArrayList<>();
                        mList = response.body().data;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                LinearLayoutManager manager = new LinearLayoutManager(AppointmentListActivity.this);
                                rvData.setLayoutManager(manager);
                                mAdapter = new OrderListAdapter(AppointmentListActivity.this, mList);
                                rvData.setAdapter(mAdapter);
                                rvData.setPullRefreshEnabled(false);
                                rvData.setLoadingMoreEnabled(false);
                                mAdapter.setViewClickListener(new OrderListAdapter.ViewClickListener() {
                                    @Override
                                    public void borrowKey(int pos) {
                                        Map<String ,Object> map=new HashMap<>();
                                        map.put("id",mList.get(pos).getId());
                                        map.put("adminid",BaseApplication.getIntance().getLoginBean().getId());

                                        Gson gg=new Gson();
                                        String json =gg.toJson(map);

                                        RequestBody body = RequestBody.create(LoginActivity.JSON,json);
                                        OkGo.<LzyResponse>post(Urls.URL_YUYUE_OPENGRID)//
                                                .tag(this)//
                                                .upRequestBody(body)
                                                .isMultipart(false)         //强制使用 multipart/form-data 表单上传（只是演示，不需要的话不要设置。默认就是false）
                                                .execute(new DialogCallback<LzyResponse>(AppointmentListActivity.this) {
                                                    @Override
                                                    public void onSuccess(Response<LzyResponse> response) {


                                                        Log.e("MainActivity","onSuccess-----logout---------------"+response.body().data);

                                                        if(null!=response.body().msg){
                                                            Toast.makeText(AppointmentListActivity.this,response.body().msg,Toast.LENGTH_LONG).show();
                                                        }

                                                    }

                                                    @Override
                                                    public void onError(Response<LzyResponse> response) {
                                                        Log.e("MainActivity","onError---------------------");
                                                        if(null!=response.body().msg){
                                                            Toast.makeText(AppointmentListActivity.this,response.body().msg,Toast.LENGTH_LONG).show();
                                                        }
                                                    }
                                                });
                                    }
                                });

                                if (mList.size() == 0) {
                                    tvNoData.setVisibility(View.VISIBLE);
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(Response<LzyResponse<List<OrderModel>>> response) {
                        Log.e("LoginActivity", "onError---------------------");
//                      startActivity(new Intent(LoginActivity.this, MainActivity.class));
                        Toast.makeText(getApplicationContext(), "无数据", Toast.LENGTH_LONG).show();
                    }
                });

    }
}
